﻿using ML.Entity;
using ML.IBusiness;
using System.Web.Mvc;

namespace ML.Web.Controllers
{
    /// <summary>
    /// 商户控制器
    /// </summary>
    [Login]
    public class MerchantController : BaseController
    {
        private readonly IMerchantBusiness merchantBusiness;
        public MerchantController(IMerchantBusiness _merchantBusiness)
        {
            merchantBusiness = _merchantBusiness;
        }

        /// <summary>
        /// 商户管理页面
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 个人商户管理页面
        /// </summary>
        /// <returns></returns>
        public ActionResult UserIndex()
        {
            return View();
        }

        /// <summary>
        /// 根据查询条件查询商户信息
        /// </summary>
        /// <param name="merchantSearch">商户查询条件</param>
        /// <returns></returns>
        [HttpGet]
        [AjaxOnly]
        public ActionResult List(MerchantSearch merchantSearch)
        {
            var data = merchantBusiness.List(merchantSearch);
            return JsonDate(data.data);
        }

        /// <summary>
        /// 根据查询条件查询登录商户下的店铺信息
        /// </summary>
        /// <param name="merchantSearch">商户查询条件</param>
        /// <returns></returns>
        [HttpGet]
        [AjaxOnly]
        public ActionResult ListUser(MerchantSearch merchantSearch)
        {
            merchantSearch.userId = UserHelper.GetCurrentUserId();
            var data = merchantBusiness.List(merchantSearch);
            return JsonDate(data.data);
        }

        /// <summary>
        /// 根据商户Id查询商户信息
        /// </summary>
        /// <param name="id">商户Id</param>
        /// <returns></returns>
        [HttpGet]
        [AjaxOnly]
        public ActionResult GetDetailById(string id)
        {
            var data = merchantBusiness.GetDetailById(id);
            return JsonDateTime(data);
        }

        /// <summary>
        /// 根据商户Id查询商户收款方式
        /// </summary>
        /// <param name="id">商户Id</param>
        /// <returns></returns>
        [HttpGet]
        [AjaxOnly]
        public ActionResult GetReceivablesByMerchantId(string id)
        {
            var data = merchantBusiness.GetReceivablesByMerchantId(id);
            return JsonDate(data);
        }

        /// <summary>
        /// 根据商户Id查询商户活动
        /// </summary>
        /// <param name="id">商户Id</param>
        /// <returns></returns>
        [HttpGet]
        [AjaxOnly]
        public ActionResult GetBusinessActivitiesByMerchantId(string id)
        {
            var data = merchantBusiness.GetBusinessActivitiesByMerchantId(id);
            return JsonDateTime(data);
        }

        /// <summary>
        /// 根据商户Id查询商户产品分类
        /// </summary>
        /// <param name="id">商户Id</param>
        /// <returns></returns>
        [HttpGet]
        [AjaxOnly]
        public ActionResult GetProductCategoryByMerchantId(string id)
        {
            var data = merchantBusiness.GetProductCategoryByMerchantId(id);
            return JsonDateTime(data);
        }

        /// <summary>
        /// 添加商户信息
        /// </summary>
        /// <param name="merchantEntity">商户实体</param>
        /// <returns></returns>
        [HttpPost]
        [AjaxOnly]
        public ActionResult Add(MerchantEntity merchantEntity)
        {
            merchantEntity.UserId = UserHelper.GetCurrentUserId();
            var data = merchantBusiness.Add(merchantEntity);
            return Json(data);
        }

        /// <summary>
        /// 修改商户信息
        /// </summary>
        /// <param name="merchantEntity">商户实体</param>
        /// <returns></returns>
        [HttpPost]
        [AjaxOnly]
        public ActionResult Edit(MerchantEntity merchantEntity)
        {
            var data = merchantBusiness.Update(merchantEntity);
            return Json(data);
        }

        /// <summary>
        ///  修改商户营业修改状态
        /// </summary>
        /// <param name="Id">商户Id</param>
        /// <param name="JoinMark">是否营业(0:未营业，1:营业中)</param>
        /// <returns></returns>
        [HttpPost]
        [AjaxOnly]
        public ActionResult Business(string Id, string JoinMark)
        {
            var data = merchantBusiness.UpdateBusinessState(Id, JoinMark);
            return Json(data);
        }

        /// <summary>
        /// 删除商户
        /// </summary>
        /// <param name="id">主键</param>
        /// <returns></returns>
        [HttpPost]
        [AjaxOnly]
        public ActionResult Delete(string id)
        {
            var data = merchantBusiness.Delete(id);
            return Json(data);
        }
    }
}